package com.easy.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public class ImageUtil {
    private static final String ENDPOINT="oss-cn-beijing.aliyuncs.com";
    private static final String BUCKET_NAME="erpandmes";
    private static final String ACCESS_KEY_ID="LTAI5tSixwkn4sLbzXLKJihn";
    private static final String ACCESS_KEY_SECRET="HVx1c7dRrpLK1cjUa1imekGHNqjyYp";
    public static String upload(InputStream is, String fileName)
    {
        String newFileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        //请求对象
        PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, newFileName, is);
        //响应对象
        PutObjectResult result = ossClient.putObject(request);
        return "https://"+BUCKET_NAME+"."+ENDPOINT+"/"+newFileName;

    }
    public static void main(String[] args) {
        try {
            MultipartFile mfile=null;
            mfile.getInputStream();
            File file = new File("D:\\JAVA学习\\FactoryLogo.jpg");
            InputStream is = new FileInputStream(file);
            String newFileName = upload(is, file.getName());
            System.out.println(newFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
